﻿using System;
using System.Text.RegularExpressions;

public class FilterUtility
{
    public static string error;

    /// <summary>
    /// Check name format
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    public static bool CheckName(string name)
    {
        if (!string.IsNullOrEmpty(name))
        {
            Regex checkMask = new Regex("^([\u4e00-\u9fa5_a-zA-Z0-9 ]+)$");
            if (checkMask.IsMatch(name))
            {
                return true;
            }
            else { error = "filter"; }
        }
        else { error = "empty"; }

        return false;
    }

    /// <summary>
    /// Check password format
    /// </summary>
    /// <param name="pass"></param>
    /// <returns></returns>
    public static bool CheckPassword(string pass)
    {
        if (!string.IsNullOrEmpty(pass))
        {
            Regex checkMask = new Regex("^([a-zA-Z0-9]+)$");
            if (checkMask.IsMatch(pass) && pass.Length >= 4)
            {
                return true;
            }
            else { error = "filter"; }
        }
        else { error = "empty"; }

        return false;
    }

    /// <summary>
    /// Check e-mail format
    /// </summary>
    /// <param name="mail"></param>
    /// <returns></returns>
    public static bool CheckMail(string mail)
    {
        if (!string.IsNullOrEmpty(mail))
        {
            Regex checkMask = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            if (checkMask.IsMatch(mail))
            {
                return true;
            }
            else { error = "filter"; }
        }
        else { error = "empty"; }

        return false;
    }

    /// <summary>
    /// Check number (check that the value contains only numbers)
    /// </summary>
    /// <param name="number"></param>
    /// <returns></returns>
    public static bool CheckNumber(string number)
    {
        if (number != null)
        {
            try
            {
                int.Parse(number);
            }
            catch (FormatException)
            {
                error = "filter";
                return false;
            }
        }
        return true;
    }

    public static bool CheckIP(string ip)
    {
        try
        {
            System.Net.IPAddress.Parse(ip);
        }
        catch (ArgumentNullException)
        {
            error = "empty";
            return false;
        }
        catch (FormatException)
        {
            error = "filter";
            return false;
        }

        return true;
    }
}
